package com.meilingcloud.research.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.SecurityUtils;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import com.meilingcloud.research.domain.RwsQcEntry;
import com.meilingcloud.research.domain.RwsQcSample;
import com.meilingcloud.research.domain.RwsQualityControl;
import com.meilingcloud.research.domain.vo.QcSampleBackupVo;
import com.meilingcloud.research.service.IRwsQualityControlService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 质控 Controller
 *
 * @author chenl
 * @date 2021-05-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/research/qualityControl")
public class RwsQualityControlController extends BaseController {

    private final IRwsQualityControlService iRwsQualityControlService;

    /**
     * 查询质控 列表
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:list')")
    @GetMapping("/list")
    public TableDataInfo list(RwsQualityControl rwsQualityControl) {
        startPage();
        List<RwsQualityControl> list = iRwsQualityControlService.queryList(rwsQualityControl);
        return getDataTable(list);
    }

    /**
     * 获取关联质控条目ids
     */
    @GetMapping("/getLinkEntryIds/{qcId}")
    public AjaxResult getLinkEntryIds(@PathVariable("qcId") Long qcId) {
        return AjaxResult.success(iRwsQualityControlService.queryLinkEntryIds(qcId));
    }

    /**
     * 获取关联质控条目
     */
    @GetMapping("/getLinkEntries/{qcId}")
    public AjaxResult getLinkEntries(@PathVariable("qcId") Long qcId) {
        return AjaxResult.success(iRwsQualityControlService.queryLinkEntries(qcId));
    }

    /**
     * 新增、修改 质控-条目
     */
    @PostMapping("/handleLinkEntry")
    @Log(title = "质控列表 ", businessType = BusinessType.OTHER)
    public AjaxResult handleLinkEntry(@RequestBody List<RwsQcEntry> qcEntries) {
        return toAjax(iRwsQualityControlService.handleLinkEntry(qcEntries));
    }

    /**
     * 获取关联样本
     */
    @GetMapping("/getLinkSample/{qcId}")
    public TableDataInfo getLinkSamples(@PathVariable("qcId") Long qcId) {
        startPage();
        List<RwsQcSample> list = iRwsQualityControlService.queryLinkSamples(qcId);
        if (CollectionUtil.isNotEmpty(list)) {
            List<QcSampleBackupVo> sampleBackups = iRwsQualityControlService.handleLinkSamplesResult(list);
            return getDataTable(sampleBackups);
        }
        return getDataTable(list);
    }

    /**
     * 获取质控报告列表
     */
    @GetMapping("/getReport/{qcId}")
    public AjaxResult getReport(@PathVariable("qcId") Long qcId) {
        return AjaxResult.success(iRwsQualityControlService.queryReport(qcId));
    }

    /**
     * 新增 质控-样本关联关系
     */
    @PostMapping("/addLinkSample")
    @Log(title = "质控列表 ", businessType = BusinessType.OTHER)
    public AjaxResult addLinkSample(@RequestBody List<RwsQcSample> qcSamples) {
        return toAjax(iRwsQualityControlService.addLinkSample(qcSamples));
    }

    /**
     * 移除 质控-样本关联关系
     */
    @PostMapping("/removeLinkSample")
    @Log(title = "质控列表 ", businessType = BusinessType.OTHER)
    public AjaxResult removeLinkSample(@RequestBody List<Long> qcSampIds) {
        return toAjax(iRwsQualityControlService.removeLinkSample(qcSampIds));
    }

    /**
     * 修改 质控关联的样本质量值
     */
    @PostMapping("/editQcSample")
    @Log(title = "质控列表 ", businessType = BusinessType.OTHER)
    public AjaxResult editQcSample(@RequestBody RwsQcSample qcSample) {
        return toAjax(iRwsQualityControlService.editQcSample(qcSample));
    }

    /**
     * 导出质控 列表
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:export')")
    @Log(title = "质控列表 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RwsQualityControl rwsQualityControl) {
        List<RwsQualityControl> list = iRwsQualityControlService.queryList(rwsQualityControl);
        ExcelUtil<RwsQualityControl> util = new ExcelUtil<RwsQualityControl>(RwsQualityControl.class);
        return util.exportExcel(list, "qualityControl");
    }

    /**
     * 获取质控 详细信息
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:query')")
    @GetMapping(value = "/{qcId}")
    public AjaxResult getInfo(@PathVariable("qcId") Long qcId) {
        return AjaxResult.success(iRwsQualityControlService.getById(qcId));
    }

    /**
     * 新增质控
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:add')")
    @Log(title = "质控列表 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid RwsQualityControl rwsQualityControl) {
        rwsQualityControl.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iRwsQualityControlService.save(rwsQualityControl));
    }

    /**
     * 修改质控
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:edit')")
    @Log(title = "质控列表 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid RwsQualityControl rwsQualityControl) {
        rwsQualityControl.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iRwsQualityControlService.updateQualityControl(rwsQualityControl));
    }

    /**
     * 删除质控
     */
    @PreAuthorize("@ss.hasPermi('research:qualityControl:remove')")
    @Log(title = "质控列表 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{qcIds}")
    public AjaxResult remove(@PathVariable Long[] qcIds) {
        return toAjax(iRwsQualityControlService.removeByIds(Arrays.asList(qcIds)));
    }
}
